﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Ebook_Organizer
{
    public partial class tabFolderExtractor : UserControl
    {
        public tabFolderExtractor()
        {
            InitializeComponent();
        }

        private void btnSource_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtSource.Text = fbd.SelectedPath;
            }
        }

        private void btnDest_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtDest.Text = fbd.SelectedPath;
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            MoveFolder(txtSource.Text);
            this.Cursor = Cursors.Default;
        }

        private void MoveFolder(string folder)
        {
            foreach (string dir in Directory.GetDirectories(folder))
                MoveFolder(dir);

            foreach (string file in Directory.GetFiles(folder))
            {
                string filename = Path.GetFileName(file);
                string dest = Path.Combine(txtDest.Text, filename);
                txtCopy.Text = filename;
                Application.DoEvents();

                if (File.Exists(dest))
                    File.Delete(file);
                else
                    File.Move(file, dest);
            }
            if (Directory.GetFiles(folder).Length == 0 && Directory.GetDirectories(folder).Length == 0)
                Directory.Delete(folder);
        }
    }
}
